﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OMap.Test.TestDomain;
using OMap.Test.TestDto;


namespace OMap.Test {
    /// <summary>
    /// Tests the Class OMap
    /// </summary>
    [TestClass]
    public class OMapTest {
        public OMapTest() {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

       
        /// <summary>
        /// Tests if an object with a flat structure e.g. no lists or subproperties could be mapped into another object.
        /// </summary>
        [TestMethod]
        public void CanMapFlatObjectFromExistingToNew() {

            Address source = GetTestSourceFlatObject();
            AddressDto target = new AddressDto();
            OMap oMap = new OMap();
            target = oMap.MapTo<Address, AddressDto>(source);

            
            ValidateAddressMapping(source, target);
        }

        /// <summary>
        /// Tests if an object with a complex structure could be mapped into another object.
        /// </summary>
        [TestMethod]
        public void CanMapComplexObjectFromExistingToNew() {

            Party source = new Party();
            source.Description = "test party";
            source.Loactions = new List<Location>();
            source.Loactions.Add(new Location(){Address = new Address(){ City = "TestCity", Street = "TestStreet", HouseNo = "00"} });
            source.Contacts = new List<Party>();
            source.Contacts.Add(new Company(){Name ="TestCompany"});
            source.Contacts.Add(new Person() { FirstName = "TestName", Description = "TestPerson", LastName = "TestLastName"});


            PartyDto target = new PartyDto();
            target.Name = "test";
            OMap oMap = new OMap();
            oMap.OverwriteTarget = true;
            oMap.AddMap(typeof(Company), typeof(CompanyDto), true);
            target = oMap.MapTo<Party, PartyDto>(source, target);

        }


        private void ValidateAddressMapping(Address source, AddressDto target) {
            Assert.IsNotNull(target);
            Assert.AreEqual(source.City,target.City,"Source property City is not equal to target property City.");
            Assert.AreEqual(source.Street, target.Street, "Source property Street is not equal to target property Street.");
            Assert.AreEqual(source.HouseNo,target.HouseNo,"Source property HouseNo is not equal to target property HouseNo.");
        }

        /// <summary>
        /// Tests if an object with a flat structure e.g. no lists or subproperties could be mapped into another object.
        /// </summary>
        [TestMethod]
        public void CanMapListOfFlatObjectFromExistingToNew() {

            List<Address> source = new List<Address>() {GetTestSourceFlatObject(),GetTestSourceFlatObject()};
            List<AddressDto> target = new List<AddressDto>();
            OMap oMap = new OMap();
            target = oMap.MapTo<List<Address>, List<AddressDto>>(source);

            foreach(var targetItem in target) {
                foreach(var sourceItem in source) {
                    ValidateAddressMapping(sourceItem,targetItem);
                }
            }
            


        }

        private static Address GetTestSourceFlatObject() {
            Address source = new Address();
            source.City = "TestCity";
            source.Street = "TestStreet";
            source.HouseNo = "TestNo";

            return source;
        }
    }
}
;